//
//  BKRightCollectionView.m
//  bestkeep
//
//  Created by utouu_mhm on 16/11/16.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKRightCollectionView.h"
#import "BKRightCollectionViewCell.h"

#define RGB(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1]
static NSString *identifier = @"BKRightCollectionViewCell";

@implementation BKRightCollectionView

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        self.layer.borderWidth = 0.25;
        self.layer.borderColor = RGB(218, 218, 218).CGColor;
        self.backgroundColor = [UIColor clearColor];
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.dataSource = self;
        self.delegate = self;
        [self registerClass:[BKRightCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        [self registerClass:[BKRightHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([BKRightHeadView class])];
    }
    return self;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    SecondChildrenList *childreList = [self.classifyrightCollArrayView objectAtIndex:section];
    NSMutableArray *mulArray = [NSMutableArray array];
    for (ThirdChildrenList *model in childreList.childrenList) {
        if (![model.hasGoodsflag isEqualToString:@"0"]) {
            
            [mulArray addObject:model];
        }
    }
    return mulArray.count;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.classifyrightCollArrayView.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BKRightCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    SecondChildrenList *secondModel = [self.classifyrightCollArrayView objectAtIndex:indexPath.section];
    NSMutableArray *mulArray = [NSMutableArray array];
    for (ThirdChildrenList *model in secondModel.childrenList) {
        if (![model.hasGoodsflag isEqualToString:@"0"]) {
            
            [mulArray addObject:model];
        }
    }
    ThirdChildrenList *model = [mulArray objectAtIndex:indexPath.item];
    cell.rightModel = model;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SecondChildrenList *secondModel = [self.classifyrightCollArrayView objectAtIndex:indexPath.section];
    NSMutableArray *mulArray = [NSMutableArray array];
    for (ThirdChildrenList *model in secondModel.childrenList) {
        if (![model.hasGoodsflag isEqualToString:@"0"]) {
            [mulArray addObject:model];
        }
    }
    ThirdChildrenList *thirdModel = [mulArray objectAtIndex:indexPath.item];
    [self pushToGoodsList:thirdModel.ID];
}
//头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    SecondChildrenList *model = [self.classifyrightCollArrayView objectAtIndex:indexPath.section];
    if (kind == UICollectionElementKindSectionHeader) {
        BKRightHeadView *rightHeadView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([BKRightHeadView class]) forIndexPath:indexPath];
        rightHeadView.titleLabel.text = model.name;
        rightHeadView.BKRightHeadViewDelegate = self;
        rightHeadView.tag = indexPath.section;
        return rightHeadView;
    }
    return nil;
}
//头视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(CGRectGetWidth(self.bounds), 45);
}

//BKRightHeadView代理方法
-(void)BKRightHeadView:(BKRightHeadView *)rightView clickView:(UIView *)clickView{
    int i = 0;
    UIView *supScetionHeaderView = clickView.superview;
    while (![supScetionHeaderView isKindOfClass:[BKRightHeadView class]]) {
        supScetionHeaderView = supScetionHeaderView.superview;
        i ++;
        if (i > 100) {//防止进入死循环
            return;
        }
    }
    if ([supScetionHeaderView isKindOfClass:[BKRightHeadView class]]) {
        SecondChildrenList *model = [self.classifyrightCollArrayView objectAtIndex:supScetionHeaderView.tag];
        [self pushToGoodsList:model.ID];
    }
}

-(void)pushToGoodsList:(NSString *)categotyId{
    if ([self.classifyRightDelegate respondsToSelector:@selector(classifyPushWithCategotyId:)]) {
        [self.classifyRightDelegate classifyPushWithCategotyId:categotyId];
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return [BKRightCollectionViewCell collectionCellSize];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (self.classifyrightCollArrayView.count > 1) {
        if (section == self.classifyrightCollArrayView.count - 1) {
            return UIEdgeInsetsMake(20, 0, 80, 0);
        }
    }
    return UIEdgeInsetsMake(20, 0, 30, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4.5f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4.5f;
}

@end
